﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.IO;
using System.Windows.Forms;
using System.Runtime.CompilerServices;

#region Copyright (C) 2010 Null Studio
/*
 * Author:Null hetaoos@gmail.com
 * Blog: http://blog.hetaoos.com
 * Copyright (C) 2010 Null Studio
 * 
 * 
 * PACMaker is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * PACMaker is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with PACMaker. If not, see <http://www.gnu.org/licenses/>.
 */
#endregion

namespace com.hetaoos.PacMaker.Configurations
{
    public class AppConfigManager
    {
        private AppConfig _appConfig;
        private object _asyncAppConfig = new object();

        /// <summary>
        /// 配置内容
        /// </summary>
        public AppConfig AppConfig
        {
            get
            {
                lock (_asyncAppConfig)
                {
                    if (_appConfig == null)
                    {
                        _appConfig = new AppConfig();
                        _appConfig.SetDefault();
                    }
                }
                return _appConfig;
            }
            set { _appConfig = value; }
        }

        private static string _configFileName = string.Format("Config.xml");
        private static AppConfigManager _instance = new AppConfigManager();

        public static AppConfigManager Instance
        {
            get { return AppConfigManager._instance; }
        }

        private AppConfigManager()
        {
            ReadConfigFile();
        }

        ~AppConfigManager()
        {
            WriteCurrentConfig();
        }


        public void ReadConfigFile()
        {
            _appConfig = AppConfig.ReadObjectFromFile(_configFileName);
            if (_appConfig == null)
            {
                _appConfig = new AppConfig();
                _appConfig.SetDefault();
                WriteCurrentConfig();
            }
        }

        public bool WriteCurrentConfig()
        {
            return this.AppConfig.WriteObjectToFile(_configFileName);
        }
    }
}
